package com.example.gby.recyclerview202gby.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.gby.recyclerview202gby.adapter.MyAdapter;
import com.example.gby.recyclerview202gby.R;
import com.example.gby.recyclerview202gby.bean.ArticleBean;
import com.example.gby.recyclerview202gby.iface.ArticleListener;
import com.example.gby.recyclerview202gby.model.ArticleModel;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;//控件
    //RecyclerView控件布局管理
    private RecyclerView.LayoutManager layoutManager;

    private MyAdapter myAdapter;//RecyclerView的适配器

    private List<ArticleBean> list=null;

    ArticleListener articleListener=new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beenlist) {
            list=beenlist;//拿到文章数据
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();//通知更新

        }

        @Override
        public void onFail(String msg) {

            Toast.makeText(MainActivity.this,"失败"+msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initRecyclerView();
        ArticleModel model=new ArticleModel();
        model.getResultList("article",1,"rv1l2tidbomann2qn86nln6ur2",articleListener);
    }
    //初始化RecyclerView
    private void initRecyclerView() {

        recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //设定每个list高度，提高性能
        recyclerView.setHasFixedSize(true);
       // list=getData();
        myAdapter=new MyAdapter(this);
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);

    }

    /*
    //获取数据原并返回list
    private List<ArticleBean> getData(){
        List<ArticleBean> list=new ArrayList<ArticleBean>();
        ArticleBean articleBean;
        //1
        articleBean=new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("标题1");
        articleBean.setTime("1111-1-1");
        articleBean.setImgid(R.drawable.f1);
        list.add(articleBean);
          //2

        articleBean=new ArticleBean();
        articleBean.setArticleId(2);
        articleBean.setTitle("标题2");
        articleBean.setTime("2222-2-22");
        articleBean.setImgid(R.drawable.f2);
        list.add(articleBean);
          //3

        articleBean=new ArticleBean();
        articleBean.setArticleId(3);
        articleBean.setTitle("标题3");
        articleBean.setTime("3333-3-33");
        articleBean.setImgid(R.drawable.f3);
        list.add(articleBean);

        //4
        articleBean=new ArticleBean();
        articleBean.setArticleId(4);
        articleBean.setTitle("标题4");
        articleBean.setTime("4444-4-44");
        articleBean.setImgid(R.drawable.f4);
        list.add(articleBean);

        //5
        articleBean=new ArticleBean();
        articleBean.setArticleId(5);
        articleBean.setTitle("标题5");
        articleBean.setTime("5555-5-55");
        articleBean.setImgid(R.drawable.f5);
        list.add(articleBean);

        //6
        articleBean=new ArticleBean();
        articleBean.setArticleId(6);
        articleBean.setTitle("标题6");
        articleBean.setTime("6666-6-66");
        articleBean.setImgid(R.drawable.f6);
        list.add(articleBean);
        return list;
    }
    */
}
